layui.config({
    base: ctx + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index',         //主入口模块
    viewer: 'viewer'
}).use(['index', 'table', 'viewer'], function () {
    var loupan = {};
    var prefix = ctx + "/manage/loupan/loupan";
    var $ = layui.$, form = layui.form, table = layui.table;

    //监听搜索
    form.on('submit(LAY-search)', function (data) {
        var field = data.field;
        var name = field.name || '';
        if (name != '') {
            field.name = '%' + name;
        }
        //执行重载
        table.reload('LAY-table', {
            where: field,
            done: function () {
                if (loupan.viewer != '' && loupan.viewer != null) {
                    loupan.viewer.destroy();
                }
                loupan.viewer = new Viewer(document.getElementById('pictures'), {
                    url: 'url'
                });
            }
        });
    });

    //事件
    var active = {
        batchdel: function () {
            // 遍历所有选择的行数据，取每条数据对应的ID
            var checkStatus = table.checkStatus('LAY-table')
                , checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return commonJs.openMsg('warning', '请选择要删除的数据！');
            }
            var ids = new Array();
            $.each(checkData, function (i, row) {
                ids[i] = row['id'];
            });

            commonJs.openConfirm('确定删除吗？', function () {
                commonJs.openAjax(prefix + "/batchRemove", {"ids": ids}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '删除成功！', function () {
                            table.reload('LAY-table'); //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        },
        add: function () {
            commonJs.openDialog('添加', prefix + '/add', {}, [], function (index, layero) {
                var iframeWindow = window['layui-layer-iframe' + index]
                    , submitID = 'LAY-operate-submit'
                    , submit = layero.find('iframe').contents().find('#' + submitID);

                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                    commonJs.openAjax(prefix + "/save", data, function (result) {
                        if (result.code == 0) {
                            commonJs.openMsg('success', '添加成功！', function () {
                                table.reload('LAY-table'); //数据刷新
                                layer.close(index);        //关闭弹层
                            });
                        } else {
                            commonJs.openMsg('error', result.msg);
                        }
                    });
                });
                submit.trigger('click');
            });
        },
        reptile: function () {
            commonJs.openConfirm('确定爬取中山楼盘的信息吗？', function () {
                commonJs.openAjax(prefix + "/reptile", {}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '爬取成功！', function () {
                            table.reload('LAY-table'); //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        },
        export: function () {
            commonJs.downloadFile(prefix + "/export");
        }
    };

    $('.layui-btn.layuiadmin-btn-operate').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    var cols = [{
        type: "checkbox",
        fixed: "left"
    }, {
        field: 'id',
        title: 'ID',
        width: 80
    }, {
        field: 'city',
        title: '城市',
        width: 80
    }, {
        field: 'district',
        title: '镇区',
        width: 80
    }, {
        field: 'name',
        title: '楼盘名称'
    }, {
        field: 'address',
        title: '地址'
    }, {
        field: 'areaRange',
        title: '建筑面积',
        width: 90
    }, {
        field: 'roomDesc',
        title: '房型',
        width: 80
    }, {
        field: 'averagePrice',
        title: '均价(元)',
        width: 90
    }, {
        field: 'averagePriceUnit',
        title: '平均单价(元)',
        width: 110
    }, {
        field: 'houseType',
        title: '房屋类型',
        width: 90
    }, {
        field: 'decoration',
        title: '房屋装饰',
        width: 90
    }, {
        field: 'saleStatus',
        title: '销售状态',
        width: 90
    }, {
        field: 'totalPriceStart',
        title: '总价起(万/套)',
        width: 120
    }, {
        field: 'coverPic',
        title: '封面图片',
        templet: function (d) {
            var coverPic = d.coverPic || '';
            if (coverPic != '' && !coverPic.startsWith("http")) {
                return '<img style="display:inline-block;width:100%;height:100%;cursor:pointer;" src=' + ctx + coverPic + '>';
            }else if(coverPic.startsWith("http")){
                return '<img style="display:inline-block;width:100%;height:100%;cursor:pointer;" src=' + coverPic + '>';
            }
            return coverPic;
        }
    }, {
        field: 'tags',
        title: '标签'
    }, {
        title: "操作",
        width: 150,
        align: "center",
        fixed: "right",
        toolbar: "#table-btn-operate"
    }];

    //初始化layui的table
    commonJs.tableRender(table, cols, prefix + "/list", null, null, null, function () {
        if (loupan.viewer != '' && loupan.viewer != null) {
            loupan.viewer.destroy();
        }
        loupan.viewer = new Viewer(document.getElementById('pictures'), {
            url: 'url'
        });
    });

    //初始化layui的table的tool操作
    commonJs.tableTool(table, prefix);


});